package com.itrui.comment.web;

import com.itrui.comment.common.Code;
import com.itrui.comment.common.Result;
import com.itrui.comment.pojo.Comment;
import com.itrui.comment.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/comment")
//添加，删除，查看
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 添加评论
     * @param comment
     * @return
     */
    @PostMapping
    public Result addComment(@RequestBody Comment comment){
        //看是否有权限评论（拉黑与否）
        boolean b = commentService.isBlack(comment.getSender(), comment.getTarget());
        //如果已经拉黑则不能评论
        if (b == true){
            return new Result(Code.SAVE_ERR, "对方已经把你拉黑");
        } else {
            //如果没有拉黑则评论
            comment.setSendTime(LocalDateTime.now());
            boolean save = commentService.save(comment);
            return save? new Result(Code.SAVE_OK, "评论成功"):new Result(Code.SAVE_ERR,"评论失败");
        }

    }

    @DeleteMapping("/{id}")
    public Result deleteComment(@PathVariable Long id){

        boolean b = commentService.delete(id,2);

        return b?new Result(Code.DELETE_OK,"删除成功"):new Result(Code.DELETE_ERR,"删除失败");

    }

    @GetMapping("/{id}/{type}")
    public Result getCommentService(@PathVariable("id") Long id, @PathVariable("type") int type) {
        List<Comment> comments = commentService.get(id, type);
        return new Result(Code.GET_OK,comments,"查询成功");

    }
}
